﻿namespace YidanSoft.Library.EditorUtility.Menus
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.Collections;

    [ToolboxItem(false)]
    public class MenuCommand : Component
    {
        protected bool _break;
        protected bool _checked;
        private EventHandler Click;
        protected System.Windows.Forms.ComboBox comboBox;
        protected bool enabled;
        protected Bitmap image;
        protected int imageIndex;
        protected System.Windows.Forms.ImageList imageList;
        protected bool infrequent;
        protected MenuCommandCollection menuItems;
        private PropChangeHandler PropertyChanged;
        protected bool radioCheck;
        protected object realOwner;
        protected System.Windows.Forms.Shortcut shortcut;
        protected string text;
        private EventHandler Update;
        protected object userData;
        protected bool visible;

        public event EventHandler Click
        {
            add
            {
                EventHandler handler2;
                EventHandler click = this.Click;
                do
                {
                    handler2 = click;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    click = Interlocked.CompareExchange<EventHandler>(ref this.Click, handler3, handler2);
                }
                while (click != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler click = this.Click;
                do
                {
                    handler2 = click;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    click = Interlocked.CompareExchange<EventHandler>(ref this.Click, handler3, handler2);
                }
                while (click != handler2);
            }
        }

        public event PropChangeHandler PropertyChanged
        {
            add
            {
                PropChangeHandler handler2;
                PropChangeHandler propertyChanged = this.PropertyChanged;
                do
                {
                    handler2 = propertyChanged;
                    PropChangeHandler handler3 = (PropChangeHandler) Delegate.Combine(handler2, value);
                    propertyChanged = Interlocked.CompareExchange<PropChangeHandler>(ref this.PropertyChanged, handler3, handler2);
                }
                while (propertyChanged != handler2);
            }
            remove
            {
                PropChangeHandler handler2;
                PropChangeHandler propertyChanged = this.PropertyChanged;
                do
                {
                    handler2 = propertyChanged;
                    PropChangeHandler handler3 = (PropChangeHandler) Delegate.Remove(handler2, value);
                    propertyChanged = Interlocked.CompareExchange<PropChangeHandler>(ref this.PropertyChanged, handler3, handler2);
                }
                while (propertyChanged != handler2);
            }
        }

        public event EventHandler Update
        {
            add
            {
                EventHandler handler2;
                EventHandler update = this.Update;
                do
                {
                    handler2 = update;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    update = Interlocked.CompareExchange<EventHandler>(ref this.Update, handler3, handler2);
                }
                while (update != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler update = this.Update;
                do
                {
                    handler2 = update;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    update = Interlocked.CompareExchange<EventHandler>(ref this.Update, handler3, handler2);
                }
                while (update != handler2);
            }
        }

        public MenuCommand()
        {
            this.InternalConstruct("MenuItem", null, -1, System.Windows.Forms.Shortcut.None, null, null, null);
        }

        public MenuCommand(string text)
        {
            this.InternalConstruct(text, null, -1, System.Windows.Forms.Shortcut.None, null, null, null);
        }

        public MenuCommand(System.Windows.Forms.ComboBox comboBox)
        {
            this.InternalConstruct("", null, -1, System.Windows.Forms.Shortcut.None, null, null, null);
            this.comboBox = comboBox;
        }

        public MenuCommand(string text, EventHandler clickHandler)
        {
            this.InternalConstruct(text, null, -1, System.Windows.Forms.Shortcut.None, clickHandler, null, null);
        }

        public MenuCommand(string text, System.Windows.Forms.Shortcut shortcut)
        {
            this.InternalConstruct(text, null, -1, shortcut, null, null, null);
        }

        public MenuCommand(string text, System.Windows.Forms.ImageList imageList, int imageIndex)
        {
            this.InternalConstruct(text, imageList, imageIndex, System.Windows.Forms.Shortcut.None, null, null, null);
        }

        public MenuCommand(string text, System.Windows.Forms.Shortcut shortcut, EventHandler clickHandler)
        {
            this.InternalConstruct(text, null, -1, shortcut, clickHandler, null, null);
        }

        public MenuCommand(string text, System.Windows.Forms.ImageList imageList, int imageIndex, System.Windows.Forms.Shortcut shortcut)
        {
            this.InternalConstruct(text, imageList, imageIndex, shortcut, null, null, null);
        }

        public MenuCommand(string text, Bitmap bitmap, System.Windows.Forms.Shortcut shortcut, EventHandler clickHandler, object realOwner)
        {
            this.InternalConstruct(text, null, -1, shortcut, clickHandler, bitmap, realOwner);
        }

        public MenuCommand(string text, System.Windows.Forms.ImageList imageList, int imageIndex, System.Windows.Forms.Shortcut shortcut, EventHandler clickHandler)
        {
            this.InternalConstruct(text, imageList, imageIndex, shortcut, clickHandler, null, null);
        }

        protected void InternalConstruct(string text, System.Windows.Forms.ImageList imageList, int imageIndex, System.Windows.Forms.Shortcut shortcut, EventHandler clickHandler, Bitmap bitmap, object realOwner)
        {
            this.text = text;
            this.imageList = imageList;
            this.imageIndex = imageIndex;
            this.shortcut = shortcut;
            this.image = bitmap;
            this.realOwner = realOwner;
            this.comboBox = null;
            if (clickHandler != null)
            {
                this.Click += clickHandler;
            }
            this.enabled = true;
            this._checked = false;
            this.radioCheck = false;
            this._break = false;
            this.userData = null;
            this.visible = true;
            this.infrequent = false;
            this.menuItems = new MenuCommandCollection();
        }

        public virtual void OnClick(EventArgs e)
        {
            if (this.Click != null)
            {
                if (this.realOwner != null)
                {
                    this.Click(this.realOwner, e);
                }
                else
                {
                    this.Click(this, e);
                }
            }
        }

        public virtual void OnUpdate(EventArgs e)
        {
            if (this.Update != null)
            {
                this.Update(this, e);
            }
        }

        public bool Break
        {
            get
            {
                return this._break;
            }
            set
            {
                if (this._break != value)
                {
                    this._break = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.Break);
                    }
                }
            }
        }

        public bool Checked
        {
            get
            {
                return this._checked;
            }
            set
            {
                if (this._checked != value)
                {
                    this._checked = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.Checked);
                    }
                }
            }
        }

        public System.Windows.Forms.ComboBox ComboBox
        {
            get
            {
                return this.comboBox;
            }
            set
            {
                if (this.comboBox != value)
                {
                    this.comboBox = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.ComboBox);
                    }
                }
            }
        }

        public bool Enabled
        {
            get
            {
                return this.enabled;
            }
            set
            {
                if (this.enabled != value)
                {
                    this.enabled = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.Enabled);
                    }
                }
            }
        }

        public Bitmap Image
        {
            get
            {
                return this.image;
            }
            set
            {
                if (this.image != value)
                {
                    this.image = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.Image);
                    }
                }
            }
        }

        public int ImageIndex
        {
            get
            {
                return this.imageIndex;
            }
            set
            {
                if (this.imageIndex != value)
                {
                    this.imageIndex = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.ImageIndex);
                    }
                }
            }
        }

        public System.Windows.Forms.ImageList ImageList
        {
            get
            {
                return this.imageList;
            }
            set
            {
                if (this.imageList != value)
                {
                    this.imageList = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.ImageList);
                    }
                }
            }
        }

        public bool Infrequent
        {
            get
            {
                return this.infrequent;
            }
            set
            {
                if (this.infrequent != value)
                {
                    this.infrequent = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.Infrequent);
                    }
                }
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public MenuCommandCollection MenuCommands
        {
            get
            {
                return this.menuItems;
            }
            set
            {
                this.menuItems.Clear();
                this.menuItems = value;
            }
        }

        public bool RadioCheck
        {
            get
            {
                return this.radioCheck;
            }
            set
            {
                if (this.radioCheck != value)
                {
                    this.radioCheck = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.RadioCheck);
                    }
                }
            }
        }

        public object RealOwner
        {
            get
            {
                return this.realOwner;
            }
            set
            {
                if (this.realOwner != value)
                {
                    this.realOwner = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.RealOwner);
                    }
                }
            }
        }

        public System.Windows.Forms.Shortcut Shortcut
        {
            get
            {
                return this.shortcut;
            }
            set
            {
                if (this.shortcut != value)
                {
                    this.shortcut = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.Shortcut);
                    }
                }
            }
        }

        public string Text
        {
            get
            {
                return this.text;
            }
            set
            {
                if (this.text != value)
                {
                    this.text = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.Text);
                    }
                }
            }
        }

        public object UserData
        {
            get
            {
                return this.userData;
            }
            set
            {
                this.userData = value;
            }
        }

        public bool Visible
        {
            get
            {
                return this.visible;
            }
            set
            {
                if (this.visible != value)
                {
                    this.visible = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, Property.Visible);
                    }
                }
            }
        }

        public delegate void PropChangeHandler(MenuCommand item, MenuCommand.Property prop);

        public enum Property
        {
            Text,
            Enabled,
            ImageIndex,
            ImageList,
            Shortcut,
            Checked,
            RadioCheck,
            Break,
            Infrequent,
            Visible,
            Image,
            RealOwner,
            ComboBox
        }
    }
}

